/**
 * 候选人，面试邀约页
 */
import React from 'react';
import { Form, Input, message, Modal } from 'antd';
import pageTabUtil from '@/utils/pageTabUtil';
import { revokeInterview } from '../service';

const { TextArea } = Input;

const RevokeModal = React.forwardRef(({ form, arrangeId }, ref) => {
    const { getFieldDecorator } = form;
    const [visible, setVisible] = React.useState(false);
    const [submitLoading, setSubmitLoading] = React.useState(false);

    React.useImperativeHandle(ref, () => ({
        show: () => setVisible(true)
    }));

    const handleSubmit = () => {
        form.validateFieldsAndScroll(async (err, values) => {
            if (!err) {
                setSubmitLoading(true);
                revokeInterview({
                    ...values,
                    arrangeId
                })
                    .then(res => {
                        const { code, msg } = res;
                        if (code === 10000) {
                            setVisible(false);
                            message.success('成功撤销面试');
                            pageTabUtil.goBack();
                        } else {
                            message.error(msg);
                        }
                    })
                    .finally(() => {
                        setSubmitLoading(false);
                    });
            }
        });
    };

    const formItemLayout = {
        labelCol: {
            span: 6
        },
        wrapperCol: {
            span: 14
        }
    };

    return (
        <Modal
            title="撤销面试"
            width={680}
            visible={visible}
            confirmLoading={submitLoading}
            onOk={handleSubmit}
            onCancel={() => setVisible(false)}
        >
            <Form {...formItemLayout} style={{ paddingTop: 24 }}>
                <Form.Item label="理由">
                    {getFieldDecorator('reason', {
                        initialValue: '',
                        rules: [
                            {
                                required: true,
                                message: '理由不能为空'
                            }
                        ]
                    })(<TextArea placeholder="请输入理由" autosize={{ minRows: 3, maxRows: 6 }} maxLength={800} />)}
                </Form.Item>
            </Form>
        </Modal>
    );
});

export default Form.create({})(RevokeModal);
